package com.lanou.projecta.tv;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lanou.projecta.R;

import java.util.List;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * Created by dllo on 16/1/21.
 */
public class TvFragmentBaseAdapter extends BaseAdapter {
    private RequestQueue queue;
    private List<BaseTv.VideoListEntity> base;
    private Context context;

    public TvFragmentBaseAdapter(List<BaseTv.VideoListEntity> base, Context context) {
        queue = Volley.newRequestQueue(context);
        this.base = base;
        this.context = context;
    }

    public void getImage(NetworkImageView imageView, String url) {
        final ImageLoader imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url) {
                return null;
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
            }
        });
        imageView.setImageUrl(url, imageLoader);

    }

    @Override
    public int getCount() {
        if (base.size() > 0) {
            return base.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        if (base.size() > 0) {
            return base.get(i);
        } else {
            return null;
        }
    }


    @Override
    public long getItemId(int i) {
        if (base.size() > 0) {
            return i;
        } else {
            return 0;
        }
    }


    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        String url = null;
        if (base != null) {
            url = base.get(i).getCover();
        }
        Handler1 handler = null;
        view = LayoutInflater.from(context).inflate(R.layout.tv_item, null);
        handler = new Handler1();
        handler.title = (TextView) view.findViewById(R.id.tv_title);
        handler.body = (TextView) view.findViewById(R.id.tv_body);
        handler.time = (TextView) view.findViewById(R.id.tv_tv_time);
        handler.play = (TextView) view.findViewById(R.id.tv_play);
        handler.videoView = (VideoView) view.findViewById(R.id.videoView);
        handler.imageView = (NetworkImageView) view.findViewById(R.id.tv_img);
        handler.delete = (ImageView) view.findViewById(R.id.tv_tv_delete);
        handler.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showShare();
            }
        });
        if (base != null) {
            final Handler1 finalHandler = handler;
            final Handler1 finalHandler1 = handler;
            handler.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finalHandler1.imageView.setVisibility(View.GONE);
                    Toast.makeText(context, "HHH", Toast.LENGTH_SHORT).show();
                    Uri uri = Uri.parse(base.get(i).getMp4_url());
                    finalHandler.videoView.setVideoURI(uri);
                    finalHandler.videoView.setMediaController(new MediaController(context));
                    finalHandler.videoView.start();
                }
            });
        }
        view.setTag(handler);
        if (base != null) {
            handler.title.setText(base.get(i).getTitle());
            handler.body.setText(base.get(i).getDescription());
            String m = String.valueOf(base.get(i).getLength()/60);
            String s = String.valueOf(base.get(i).getLength()%60);
            handler.time.setText(String.valueOf(m+":"+s));
            handler.play.setText(String.valueOf(base.get(i).getPlayCount()));
            getImage(handler.imageView, url);

        }


        return view;
    }
    private void showShare() {
        ShareSDK.initSDK(context);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(context.getString(R.string.ssdk_oks_share));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(context.getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(context);
    }
    class Handler1 {
        VideoView videoView;
        NetworkImageView imageView;
        TextView title;
        TextView body;
        TextView time;
        TextView play;
        ImageView delete;
    }
}
